﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;

namespace OrionBank.Classe.SQLite
{
    public class Utilisateur
    {
        static public void Delete(int id)
        {            
            try
            {
                SQLiteCommand cmd = new SQLiteCommand(Classe.SQLite.Sql.UTILISATEURS_DELETE_ID, SQLite.Sql.GetConnection());

                cmd.Prepare();
                cmd.Parameters.AddWithValue("@id", id);
                cmd.ExecuteNonQuery();
            }
            catch (SQLiteException ex)
            {
                throw new Exception(String.Format(Classe.SQLite.Messages.SQLite_ERROR_GENERAL, ex.ErrorCode, ex.Message));
            }
        }

        static public void Delete(Classe.Utilisateur u)
        {
            Utilisateur.Delete(u.Id);
        }

        static public Classe.Utilisateur Charge(int id)
        {
            Classe.Utilisateur u = new Classe.Utilisateur();
            try
            {
                SQLiteCommand cmd = new SQLiteCommand(Classe.SQLite.Sql.UTILISATEURS_ID, SQLite.Sql.GetConnection());
                
                cmd.Prepare();
                cmd.Parameters.Add("@id", DbType.Int32);
                cmd.Parameters["@id"].Value = id;

                SQLiteDataReader rdr = cmd.ExecuteReader();
                if (rdr.Read())
                {
                    u.Id = rdr.GetInt32(0);
                    u.Login = rdr.GetString(1);
                    u.Mdp = rdr.GetString(2);
                }
                rdr.Close();
            }
            catch (SQLiteException ex)
            {
                throw new Exception(String.Format(Classe.SQLite.Messages.SQLite_ERROR_GENERAL, ex.ErrorCode, ex.Message));
            }

            return u;
        }

        static public Classe.Utilisateur Charge(string login)
        {
            Classe.Utilisateur u = new Classe.Utilisateur();
            try
            {
                SQLiteCommand cmd = new SQLiteCommand(SQLite.Sql.UTILISATEURS_LOGIN, SQLite.Sql.GetConnection());
                
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@login", login);
                
                SQLiteDataReader rdr = cmd.ExecuteReader();
                if (rdr.Read())
                {
                    u.Id = rdr.GetInt32(0);
                    u.Login = rdr.GetString(1);
                    u.Mdp = rdr.GetString(2);
                }
                rdr.Close();
            }
            catch (SQLiteException ex)
            {
                throw new Exception(String.Format(Classe.SQLite.Messages.SQLite_ERROR_GENERAL, ex.ErrorCode, ex.Message));
            }
            return u;
        }

        static public void Maj(Classe.Utilisateur u)
        {            
            try
            {
                SQLiteCommand cmd = new SQLiteCommand(SQLite.Sql.UTILISATEURS_UPDATE_ID, SQLite.Sql.GetConnection());
                
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@id", u.Id);
                cmd.Parameters.AddWithValue("@login", u.Login);
                cmd.Parameters.AddWithValue("@mdp", u.Mdp);
                cmd.ExecuteNonQuery();
            }
            catch (SQLiteException ex)
            {
                throw new Exception(String.Format(Classe.SQLite.Messages.SQLite_ERROR_GENERAL, ex.ErrorCode, ex.Message));
            }
        }

        static public void Sauve(Classe.Utilisateur u)
        {
            try
            {
                SQLiteCommand cmd = new SQLiteCommand(SQLite.Sql.UTILISATEURS_INSERT, SQLite.Sql.GetConnection());
                
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@login", u.Login);
                cmd.Parameters.AddWithValue("@mdp", u.Mdp);
                cmd.ExecuteNonQuery();
            }
            catch (SQLiteException ex)
            {
                throw new Exception(String.Format(Classe.SQLite.Messages.SQLite_ERROR_GENERAL, ex.ErrorCode, ex.Message));
            }
        }
    }
}
